package cn.iocoder.yudao.module.scrm.controller.admin.marketing;

import cn.iocoder.yudao.module.scrm.controller.admin.marketing.vo.look.MarketingLookPageReqVO;
import cn.iocoder.yudao.module.scrm.controller.admin.marketing.vo.look.MarketingLookRespVO;
import cn.iocoder.yudao.module.scrm.controller.admin.marketing.vo.look.MarketingLookSaveReqVO;
import cn.iocoder.yudao.module.scrm.dal.dataobject.marketing.MarketingLookDO;
import cn.iocoder.yudao.module.scrm.service.marketing.look.MarketingLookService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;


@Tag(name = "管理后台 - 营销浏览")
@RestController
@RequestMapping("/scrm/marketing-look")
@Validated
public class MarketingLookController {

    @Resource
    private MarketingLookService marketingLookService;

    @PostMapping("/create")
    @Operation(summary = "创建营销浏览")
    @PreAuthorize("@ss.hasPermission('scrm:marketing-look:create')")
    public CommonResult<Integer> createMarketingLook(@Valid @RequestBody MarketingLookSaveReqVO createReqVO) {
        return success(marketingLookService.createMarketingLook(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新营销浏览")
    @PreAuthorize("@ss.hasPermission('scrm:marketing-look:update')")
    public CommonResult<Boolean> updateMarketingLook(@Valid @RequestBody MarketingLookSaveReqVO updateReqVO) {
        marketingLookService.updateMarketingLook(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除营销浏览")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('scrm:marketing-look:delete')")
    public CommonResult<Boolean> deleteMarketingLook(@RequestParam("id") Integer id) {
        marketingLookService.deleteMarketingLook(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得营销浏览")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('scrm:marketing-look:query')")
    public CommonResult<MarketingLookRespVO> getMarketingLook(@RequestParam("id") Integer id) {
        MarketingLookDO marketingLook = marketingLookService.getMarketingLook(id);
        return success(BeanUtils.toBean(marketingLook, MarketingLookRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得营销浏览分页")
    @PreAuthorize("@ss.hasPermission('scrm:marketing-look:query')")
    public CommonResult<PageResult<MarketingLookRespVO>> getMarketingLookPage(@Valid MarketingLookPageReqVO pageReqVO) {
        PageResult<MarketingLookDO> pageResult = marketingLookService.getMarketingLookPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, MarketingLookRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出营销浏览 Excel")
    @PreAuthorize("@ss.hasPermission('scrm:marketing-look:export')")
    @OperateLog(type = EXPORT)
    public void exportMarketingLookExcel(@Valid MarketingLookPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<MarketingLookDO> list = marketingLookService.getMarketingLookPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "营销浏览.xls", "数据", MarketingLookRespVO.class,
                        BeanUtils.toBean(list, MarketingLookRespVO.class));
    }

}